﻿using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using CuaHangLego.Webform.App_Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CuaHangLego.Webform
{
    public partial class Default : BasePage
    {
        IList<SanPham> listSP;
        IList<SanPham> listSP_temp;
        int RowCount;
        const int PageCount = 9;
        int position;
        PagedDataSource pds;

        protected void Page_Load(object sender, EventArgs e)
        {
            NoResultLable.Visible = true;
            if (Session["ViewType"] == null)
                Session["ViewType"] = 0;
            string themeID = Request.QueryString["ThemeID"];
            string key = Request.QueryString["search"];
            if (string.IsNullOrEmpty(themeID))
            {
                if (string.IsNullOrEmpty(key))
                    listSP = SanPhamDA.Instance.GetAll();
                else listSP = SanPhamDA.Instance.GetAll().Where(sp => sp.Ten.ToLower().Contains(key)).ToList();
            }
            else
            {
                listSP = SanPhamDA.Instance.GetByTheme(int.Parse(themeID));
            }

            if (!IsPostBack)
            {
                listSP_temp = listSP;
                Session["temp"] = listSP;
            }
            ViewState["page"] = 0;
            FetchData();

            position = (int)ViewState["page"];
        }

        protected void listSP1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
            }
        }

        protected void listSP1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string ID=((HiddenField)e.Item.FindControl("ProductID")).Value;
                ((HyperLink)e.Item.FindControl("ProductLink")).NavigateUrl = "ProductDetail.aspx?ID=" + ID;
            }
        }

        private void FetchData()
        {
            listSP_temp = Session["temp"] as IList<SanPham>;
            RowCount = listSP_temp.Count;
            if (RowCount > 0)
            {
                NoResultLable.Visible = false;
            }
            else NoResultLable.Visible = true;
            
            CreatePagingControl();

            pds = new PagedDataSource();
            pds.DataSource = listSP_temp;
            pds.AllowPaging = true;
            pds.CurrentPageIndex = position;
            pds.PageSize = PageCount;
            DanhsachSanPham2.DataSource = null;
            DanhsachSanPham2.DataBind();
            DanhsachSanPham.DataSource = null;
            DanhsachSanPham.DataBind();

            switch ((int)Session["ViewType"])
            {
                case 0:
                    DanhsachSanPham.DataSource = pds;
                    DanhsachSanPham.DataBind();
                    break;
                case 1:
                    DanhsachSanPham2.DataSource = pds;
                    DanhsachSanPham2.DataBind();
                    break;
                default:
                    break;
            }
            
            
        }

        private void CreatePagingControl()
        {
            plcPaging.Controls.Clear();
            if(RowCount>0)
                for (int i = 0; i < ((RowCount - 1) / PageCount) + 1; i++)
                {
                    LinkButton lnk = new LinkButton();
                    lnk.CssClass = "PageButton";
                    lnk.EnableTheming = false;
                    lnk.Click += new EventHandler(lbl_Click);
                    lnk.ID = "lnkPage" + (i + 1).ToString();
                    lnk.Text = (i + 1).ToString();
                    plcPaging.Controls.Add(lnk);
                }
        }

        private void lbl_Click(object sender, EventArgs e)
        {
            position = Convert.ToInt32(((LinkButton)sender).Text)-1;
            ViewState["page"] = position;
            FetchData();
        }


        protected void GioHang_Command(object sender, CommandEventArgs e)
        {
            IList<int> li = Session["cart"] as List<int>;
            if (li == null)
                li = new List<int>();
            li.Add(Convert.ToInt32(e.CommandArgument.ToString()));
            li = li.Distinct().ToList();
            Session["cart"] = li;
            Response.Redirect("~/ShoppingCart.aspx");
        }

        protected void SortBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            filter();
        }

        protected void PriceGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            filter();
        }

        protected void PieceGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            filter();
        }

        private void filter()
        {
            listSP_temp = listSP;
            int type = int.Parse(SortBy.SelectedValue);
            switch (type)
            {
                case 1:
                    listSP_temp = listSP_temp.OrderBy(sp => sp.GiaBan).ToList();
                    break;
                case 2:
                    listSP_temp = listSP_temp.OrderByDescending(sp => sp.GiaBan).ToList();
                    break;
                case 3:
                    listSP_temp = listSP_temp.OrderBy(sp => sp.Ten).ToList();
                    break;
                case 4:
                    listSP_temp = listSP_temp.OrderByDescending(sp => sp.Ten).ToList();
                    break;
                default:
                    break;
            }

            type = int.Parse(PriceGroup.SelectedValue);
            switch (type)
            {
                case 1:
                    listSP_temp = listSP_temp.Where(sp => sp.GiaBan < 500).ToList();
                    break;
                case 2:
                    listSP_temp = listSP_temp.Where(sp => sp.GiaBan >= 500 && sp.GiaBan < 1000).ToList();
                    break;
                case 3:
                    listSP_temp = listSP_temp.Where(sp => sp.GiaBan >= 1000 && sp.GiaBan < 5000).ToList();
                    break;
                case 4:
                    listSP_temp = listSP_temp.Where(sp => sp.GiaBan >= 5000 && sp.GiaBan < 10000).ToList();
                    break;
                case 5:
                    listSP_temp = listSP_temp.Where(sp => sp.GiaBan >= 10000).ToList();
                    break;
                default:
                    break;
            }

            type = int.Parse(PieceGroup.SelectedValue);
            switch (type)
            {
                case 1:
                    listSP_temp = listSP_temp.Where(sp => sp.SoManhGhep < 50).ToList();
                    break;
                case 2:
                    listSP_temp = listSP_temp.Where(sp => sp.SoManhGhep >= 50 && sp.SoManhGhep < 100).ToList();
                    break;
                case 3:
                    listSP_temp = listSP_temp.Where(sp => sp.SoManhGhep >= 100 && sp.SoManhGhep < 500).ToList();
                    break;
                case 4:
                    listSP_temp = listSP_temp.Where(sp => sp.SoManhGhep >= 500 && sp.SoManhGhep < 1000).ToList();
                    break;
                case 5:
                    listSP_temp = listSP_temp.Where(sp => sp.SoManhGhep >= 1000).ToList();
                    break;
                default:
                    break;
            }
            Session["temp"] = listSP_temp;
            FetchData();
        }


        protected void GridViewButton_Click(object sender, ImageClickEventArgs e)
        {
            Session["ViewType"] = 0;
            FetchData();
        }

        protected void ListViewButton_Click(object sender, ImageClickEventArgs e)
        {
            Session["ViewType"] = 1;
            FetchData();
        }

        protected void DanhsachSanPham2_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void DanhsachSanPham2_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string ID = ((HiddenField)e.Item.FindControl("ProductID2")).Value;
                ((HyperLink)e.Item.FindControl("ListViewProductLink")).NavigateUrl = "ProductDetail.aspx?ID=" + ID;

            }
        }

    }
}